﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DLL_Downloader
{
    /// <summary>
    /// Interaction logic for Interface.xaml
    /// </summary>
    public partial class Interface : UserControl
    {
        PowerPatcherInterop.InitInfo _info;
        public Interface(PowerPatcherInterop.InitInfo info)
        {
            _info = info;
            InitializeComponent();
        }

        private void SelectAll(object sender, RoutedEventArgs e)
        {
            foreach (CheckBox i in filesList.Items)
                i.IsChecked = true;
        }

        private void SelectNone(object sender, RoutedEventArgs e)
        {
            foreach (CheckBox i in filesList.Items)
                i.IsChecked = false;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (string s in DownloadDlls.DllTable.Values)
            {
            	CheckBox c = new CheckBox();
                c.IsChecked = false;
            	c.Content = s;
            	c.Margin = new Thickness(4,0,4,0);
            	filesList.Items.Add(c);
            }
        }

        private void Download(object sender, RoutedEventArgs e)
        {
            _info.SetPatchState.Invoke(true);
            try
            {
                List<string> files = new List<string>(); //todo: LinQ
                foreach (CheckBox i in filesList.Items)
                    if (i.IsChecked == true)
                        files.Add((string)i.Content);
                DownloadDlls.DownloadNewFiles(files);
            }
            catch (Exception ex)
            {
                bool ShowDeatiled = _info.GetShowDetailedError.Invoke();
                _info.LogString("Error occured while attemping to download DLLs: " + ((ShowDeatiled) ? ex.ToString() : ex.Message), true);
            }
            finally
            {
                _info.SetPatchState.Invoke(false);
            }
        }
    }


    class DownloadDlls
    {
        //TODO: Flip this dictionary around...
        public static Dictionary<string, string> DllTable = new Dictionary<string, string>() {
        {"D01",  "bdcap32.dll"},
        {"D02",  "binkw32.dll"},
        {"D03",  "Dbghelp.dll"},
        {"D04",  "ESL.dll"},
        {"D05",  "EXL.dll"},
        {"D06",  "ijl15.dll"},
        {"D07",  "Mint.dll"},
        {"D08",  "Mss32.dll"},
        {"D09",  "msvcp80.dll"},
        {"D10",  "msvcp90.dll"},
        {"D11",  "msvcr80.dll"},
        {"D12",  "msvcr90.dll"},
        {"D13",  "nmcogame.dll"},
        {"D14",  "nmconew.dll"},
        {"D15",  "Oasis.dll"},
        {"D21",  "PathEngine.dll"},
        {"D16",  "Pleione.dll"},
        {"D17",  "Renderer2.dll"},
        {"D18",  "Skill.dll"},
        {"D19",  "Standard.dll"},
        {"D20",  "unicows.dll"}   
        };
        public static void DownloadNewFiles(List<string> files)
        {
            byte[] b;
            using (System.IO.Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("DLL_Downloader.Downloader.exe"))
            {
                b = new byte[s.Length];
                s.Read(b, 0, (int)s.Length);
            }
            System.Reflection.Assembly a = System.Reflection.Assembly.Load(b);
            System.Reflection.MethodInfo m = a.EntryPoint;
            List<string> args = new List<string>();
            foreach (KeyValuePair<string, string> k in DllTable)
            {
                if (files.Contains(k.Value))
                {
                    args.Add(k.Key + "|" + k.Value);
                }
            }
            object o = a.CreateInstance(m.Name);
            m.Invoke(o, new object[] { new string[] { string.Join("|", args.ToArray()) } });
        }
    }

}
